package com.car.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.car.account.controller.form.addressForm.InsertAddressForm;
import com.car.account.controller.form.addressForm.UpdateAddressForm;
import com.car.account.dto.addressDto.AddressByUserIdDto;
import com.car.account.entity.Address;
import com.car.account.param.addressParam.InsertAddressParam;
import com.car.account.param.addressParam.UpdateAddressParam;
import com.car.account.service.AddressService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/address")
@Api(tags = "用户地址")
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/selectByUserId")
    @ApiOperation(value = "根据用户id查询地址")
    public Result<List<AddressByUserIdDto>> selectByUserId() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        List<AddressByUserIdDto> addressByUserIdDtos = addressService.selectByUserId(userId);
        return Result.getListResult(AddressByUserIdDto.class)
                .setData(addressByUserIdDtos).setMsg("根据用户id查询地址成功");
    }

    @GetMapping("/selectByAddressId")
    @ApiOperation(value = "根据地址id查询地址")
    public Result<AddressByUserIdDto> selectByAddressId(Integer addressId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        AddressByUserIdDto addressByUserIdDto = addressService.selectByAddressId(userId, addressId);
        return Result.getResult(AddressByUserIdDto.class)
                .setData(addressByUserIdDto).setMsg("根据地址id查询地址成功");
    }

    @PostMapping("/insertByUserId")
    @ApiOperation(value = "用户新增地址")
    public Result insertByUserId(@RequestBody InsertAddressForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        InsertAddressParam param = BeanUtil.toBean(form, InsertAddressParam.class);
        param.setUserId(userId);
        addressService.insertByUserId(param);
        return Result.getResult().setMsg("新增地址成功");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "用户根据地址id删除地址")
    public Result deleteById(Integer addressId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        addressService.deleteById(userId, addressId);
        return Result.getResult().setMsg("删除地址成功");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "用户根据地址id更新地址信息")
    public Result updateById(@RequestBody UpdateAddressForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        UpdateAddressParam param = BeanUtil.toBean(form, UpdateAddressParam.class);
        param.setUserId(userId);
        addressService.updateAddressById(param);
        return Result.getResult().setMsg("修改地址信息成功");
    }

}

